{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e501601",
   "metadata": {},
   "outputs": [],
   "source": [
    "#! pip install branca==0.4.1 #0.3.1\n",
    "#! pip install wordcloud\n",
    "\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "import seaborn as sns\n",
    "import os\n",
    "import string\n",
    "import re\n",
    "from datetime import datetime\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import branca.colormap as cm\n",
    "# from mpl_toolkits.basemap import Basemap\n",
    "import requests\n",
    "import folium\n",
    "from folium import plugins\n",
    "from folium.plugins import HeatMap\n",
    "import branca.colormap\n",
    "from nltk.tokenize import TweetTokenizer\n",
    "from nltk.corpus import stopwords\n",
    "from nltk import pos_tag, ne_chunk\n",
    "from nltk.sentiment.vader import SentimentIntensityAnalyzer\n",
    "from wordcloud import WordCloud\n",
    "from tqdm import tqdm, notebook\n",
    "from iso3166 import countries\n",
    "import plotly.express as px\n",
    "%matplotlib inline\n",
    "\n",
    "pd.set_option('display.max_columns', None)\n",
    "pd.set_option('display.max_rows', None)\n",
    "pd.set_option('display.max_colwidth', None)\n",
    "pd.set_option('display.width', None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd833ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "covid = 'sa.csv'\n",
    "df = pd.read_csv(covid, index_col=0)\n",
    "df['date'] = pd.to_datetime(df['date']) \n",
    "df = df.sort_values(['date'])\n",
    "df['day'] = df['date'].astype(str).str.split(' ', expand=True)[0]\n",
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8f2c29e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74ed6506",
   "metadata": {},
   "outputs": [],
   "source": [
    "hashtags_country = df.groupby(['day', 'user_location'])['user_name'].count().reset_index()\n",
    "hashtags_country.columns = ['day', 'location', 'count']\n",
    "\n",
    "hashtags_country['location'] = hashtags_country['location'].str.split(',', expand=True)[1].str.lstrip().str.rstrip()\n",
    "country_dict = {}\n",
    "for c in countries:\n",
    "    country_dict[c.name] = c.alpha3\n",
    "    \n",
    "hashtags_country['alpha3'] = hashtags_country['location']\n",
    "hashtags_country = hashtags_country.replace({\"alpha3\": country_dict})\n",
    "\n",
    "country_list = ['England', 'United States', 'United Kingdom', 'London', 'UK']\n",
    "\n",
    "hashtags_country = hashtags_country[\n",
    "    (hashtags_country['alpha3'] == 'USA') | \n",
    "    (hashtags_country['location'].isin(country_list)) | \n",
    "    (hashtags_country['location'] != hashtags_country['alpha3'])\n",
    "]\n",
    "\n",
    "gbr = ['England', 'United Kingdom', 'London', 'UK']\n",
    "us = ['United States', 'NY', 'CA', 'GA']\n",
    "\n",
    "hashtags_country = hashtags_country[hashtags_country['location'].notnull()]\n",
    "hashtags_country.loc[hashtags_country['location'].isin(gbr), 'alpha3'] = 'GBR'\n",
    "hashtags_country.loc[hashtags_country['location'].isin(us), 'alpha3'] = 'USA'\n",
    "\n",
    "hashtags_country.loc[hashtags_country['alpha3'] == 'USA', 'location'] = 'USA'\n",
    "hashtags_country.loc[hashtags_country['alpha3'] == 'GBR', 'location'] = 'United Kingdom'\n",
    "hashtags_country = hashtags_country.groupby(['day', 'location', 'alpha3'])['count'].sum().reset_index()\n",
    "hashtags_country\n",
    "hashtags_country.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "098624de",
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_hashtag_map(data):\n",
    "    fig = px.choropleth(\n",
    "        data, \n",
    "        locations=\"alpha3\",\n",
    "        hover_name=\"count\",\n",
    "        color=\"count\",\n",
    "        animation_frame=\"day\",\n",
    "        projection=\"natural earth\",\n",
    "        color_continuous_scale=px.colors.sequential.Plasma,\n",
    "#         title='Dynamic of hashtag \"' + hashtag + '\"' ,\n",
    "        width=800, \n",
    "        height=600\n",
    "    )\n",
    "    fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d0f293a",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_hashtag_map(hashtags_country)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
